﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace Xman.Extensions.Core
{
    public static class EnumExtensions
    {
        public static string ToDescription(this Enum e)
        {
            var field = e.GetType().GetField(e.ToString());
            var objs = field.GetCustomAttribute(typeof(DescriptionAttribute));
            var descriptionAttribute = (DescriptionAttribute)objs;
            return descriptionAttribute.Description;
        }

        public static List<EnumEntity> GetEnumList(this Type enumType)
        {
            return (from object? e in Enum.GetValues(enumType) let objArr = e.GetType().GetField(e.ToString()!)!.GetCustomAttributes(typeof(DescriptionAttribute), true) let desction = objArr.Length > 0 ? (objArr[0] as DescriptionAttribute)?.Description : "" select new EnumEntity { EnumValue = Convert.ToInt32(e), EnumName = e.ToString(), EnumDescription = desction }).ToList();
        }
    }
}
